package com.plugin.manage.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plugin.core.model.entity.ElderConsultation;
import com.plugin.manage.mapper.ConsultationMapper;
import com.plugin.manage.common.PageResult;
import com.plugin.manage.service.IConsultationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 咨询记录服务实现类
 *
 * @author mark
 * @date 2025-07-30
 */
@Service
public class ConsultationServiceImpl extends ServiceImpl<ConsultationMapper, ElderConsultation> implements IConsultationService {

    @Autowired
    private ConsultationMapper consultationMapper;

    /**
     * 获取咨询记录总数
     * 
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 咨询记录总数
     */
    @Override
    public Long getTotalConsultationCount(LocalDateTime startTime, LocalDateTime endTime) {
        return consultationMapper.countTotalConsultations(startTime, endTime);
    }

    /**
     * 获取正在跟进中的咨询记录数量
     * 
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 正在跟进中的咨询记录数量
     */
    @Override
    public Long getActiveConsultationCount(LocalDateTime startTime, LocalDateTime endTime) {
        return consultationMapper.countActiveConsultations(startTime, endTime);
    }

    /**
     * 获取已签约的咨询记录数量
     * 
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 已签约的咨询记录数量
     */
    @Override
    public Long getSignedConsultationCount(LocalDateTime startTime, LocalDateTime endTime) {
        return consultationMapper.countSignedConsultations(startTime, endTime);
    }

    /**
     * 获取咨询整体转化率
     * 
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 转化率（百分比）
     */
    @Override
    public Double getConsultationConversionRate(LocalDateTime startTime, LocalDateTime endTime) {
        Long totalCount = consultationMapper.countTotalConsultations(startTime, endTime);
        Long signedCount = consultationMapper.countSignedConsultations(startTime, endTime);
        
        if (totalCount == null || totalCount == 0) {
            return 0.0;
        }
        
        // 计算转化率并保留两位小数
        double rate = (signedCount.doubleValue() / totalCount.doubleValue()) * 100;
        return Math.round(rate * 100.0) / 100.0;
    }

    /**
     * 获取销售人员咨询统计TOP5
     * 
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 销售人员咨询统计TOP5
     */
    @Override
    public List<Map<String, Object>> getSalesPerformanceTop5(LocalDateTime startTime, LocalDateTime endTime) {
        return consultationMapper.getSalesPerformanceTop5(startTime, endTime);
    }

    /**
     * 获取媒介渠道统计信息
     * 
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 媒介渠道统计信息
     */
    @Override
    public List<Map<String, Object>> getChannelStatistics(LocalDateTime startTime, LocalDateTime endTime) {
        return consultationMapper.getChannelStatistics(startTime, endTime);
    }

    @Override
    public List<Map<String, Object>> getDashboardList(String consultantName, Integer status, String saleName, 
                                                      LocalDateTime startTime, LocalDateTime endTime) {
        return consultationMapper.getDashboardList(consultantName, status, saleName, startTime, endTime);
    }
    
    @Override
    public PageResult<Map<String, Object>> getDashboardListPaged(String consultantName, Integer status, String saleName, 
                                                                LocalDateTime startTime, LocalDateTime endTime,
                                                                Long current, Long size) {
        // 创建MyBatis-Plus分页对象
        Page<Map<String, Object>> page = new Page<>(current, size);
        
        // 调用Mapper分页查询方法，MyBatis-Plus分页插件会自动处理分页逻辑
        Page<Map<String, Object>> resultPage = (Page<Map<String, Object>>) consultationMapper.getDashboardListPaged(
                page, consultantName, status, saleName, startTime, endTime);
        
        // 转换为自定义的PageResult对象
        return PageResult.of(resultPage);
    }
}
